#include "Lock.h"

int MyCondition::Wait(uint64 timeout)   
{
#if defined(__WINDOWS__)

	EnterCriticalSection(&criticalSection);
	return SleepConditionVariableCS(&conditionVarible,&criticalSection, (DWORD)timeout);

#elif defined(__LINUX__)

	pthread_mutex_lock(&m_Mutex); 
	if ( timeout == 0)
	{
		return pthread_cond_wait(&m_Cond,&m_Mutex);
	}
	else
	{
		struct timespec ts;
		struct timezone tz;
		gettimeofday(&ts, &tz);
		ts.tv_sec += timeout/1000;
		ts.tv_nsec+= (timeout%1000)*1000*1000;
		return pthread_cond_timedwait(&m_Cond,&m_Mutex,&ts);
	}

#endif
}
int MyCondition::Free()
{
#if defined(__WINDOWS__)

	LeaveCriticalSection(&criticalSection);
	return 1;

#elif defined(__LINUX__)

	return pthread_mutex_unlock(&m_Mutex);

#endif
}

int MyCondition::Get()
{
#if defined(__WINDOWS__)

	EnterCriticalSection(&criticalSection);
	return 1;

#elif defined(__LINUX__)

	return pthread_mutex_lock(&m_Mutex);

#endif
}

int MyCondition::Notify()
{
#if defined(__WINDOWS__)

	LeaveCriticalSection(&criticalSection);
	WakeConditionVariable(&conditionVarible);
	return 1;

#elif defined(__LINUX__)

	int retM = pthread_mutex_unlock(&m_Mutex);
	int retC = pthread_cond_signal(&m_Cond);
	return retM*10000+retC;

#endif
}